//
//  ZWStatusesViewModel.swift
//  ZWWeibo
//
//  Created by flyiron on 16/4/21.
//  Copyright © 2016年 flyiron. All rights reserved.
//

import UIKit

class ZWStatusesViewModel: NSObject {
    var status:ZWStatuses? {
        didSet{
            memberImage()
            reweetTextPiece(status?.retweeted_status)
            reweetPictureViewSize = calcPictureViewSize(status?.retweeted_status?.pic_urls)
            originalPictureViewSize = calcPictureViewSize(status?.pic_urls)
        }
    }
    
    var memberLevelImage:UIImage?
    var retweetText:String?
    var messageSource:String?
    var originalPictureViewSize:CGSize?
    var reweetPictureViewSize:CGSize?
 
    private func source(){
        
    }
    
    private func reweetTextPiece(model:ZWStatuses?){
        if let re = model{
            if let name = re.user?.name , content = re.text{
                retweetText = "@\(name):\(content)"
            }
        }
    }
    
    private func memberImage(){
        guard let rank = status?.user?.mbrank else {
            memberLevelImage = nil
            return
        }
        if rank > 0 && rank < 7 {
            memberLevelImage = UIImage(named: "common_icon_membership_level\(rank)")
        }
    }
    
    private func calcPictureViewSize(pic_urls:[ZWStatusPhotoInfo]?) -> CGSize{
        let count = pic_urls?.count ?? 0
        if count == 0 {
            return CGSizeZero
        }
        let col = count > 3 ? 3 : count
        let row = ((count - 1) / 3) + 1
        
        let margin: CGFloat = 5
        let itemWH = (SCREENW - 2 * ZWStatusCellMargin - 2 * margin) / 3
        
        // 3. 通过每一张图片的大小和列数求出宽度，行数求出高度
        let width = CGFloat(col) * itemWH + CGFloat(col - 1) * margin
        let height = CGFloat(row) * itemWH + CGFloat(row - 1) * margin
        return CGSize(width: width, height: height)
        
    }
}
